<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>role</title>
    <link rel="stylesheet" type="text/css" href="../lib/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../lib/themes/icon.css">
	<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
	<script type="text/javascript" src="../lib/jquery.min.js"></script>
	<script type="text/javascript" src="../lib/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../lib/datagrid-filter.js"></script>
	<script type="text/javascript" src="../lib/locale/easyui-lang-zh_CN.js"></script>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
	<script type="text/javascript" src="../lib/jiekou.js"></script>
	<script type="text/javascript" src="../lib/tools.js"></script>
	<script type="text/javascript" src="../lib/base64.js"></script>
	<link href="../lib/mask/jquery.loadmask.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="../lib/mask/jquery.loadmask.min.js"></script>
</head>
<body>
	<div id="win" class="easyui-window" title="角色操作" closed="true" style="width:600px;height:99%;padding:30px 60px;">
		<form id="ff" method="post">
			<div style="text-align:center;padding:5px 0;margin-bottom: 10px; margin-top: 0 auto;">
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="role.add()" style="width:80px;">确定</a>
			</div>
			<div style="margin-bottom:20px">
				<input type="hidden" id="roleid" data="">
				<input class="easyui-textbox" id="rolename"  style="width:100%" data-options="label:'角色名字:'">
			</div>
		
			<div style="margin-bottom:20px">
				<div class="easyui-panel" style="padding:5px">
					<ul id="tree_menu11" class="easyui-tree" ></ul>
				</div>
			</div>
		
		</form>
			  
	
</div>


	<table id=roleGrid></table>

	<div id="roletab">
		<a href="javascript:void(0)" onclick="role.openadd()" class="easyui-linkbutton" data-options="iconCls:'icon-add'">新增角色</a>
	</div>

	<script type="text/javascript">

	var role={
		openadd:function(){
			$("#tree_menu11").tree({
					url: Network.geturl("/v1/listmenu"),
					animate:true,
					method:'post',
					animate:true,
					checkbox:true,
					onLoadError:function(err){
                          $.messager.alert('tree','网络故障');
                    }          
		});
		// $('#tree_menu11').tree('reload');
		$("#rolename").textbox("setValue","");
		$("#roleid").attr("data","");
		$('#win').window('open');	
		},
		add:function(){
			var  roleid=$("#roleid").attr("data");
			console.log(roleid);
			if(roleid){
				this.update();
				return;
			}
			//start"indeterminate"
			var nodes = $('#tree_menu11').tree('getChecked');
			var s = '';
			for(var i=0; i<nodes.length; i++){
				if(nodes[i].isdir=="1") continue;
				if (s != '') s += ',';
				s += nodes[i].id;
			}
           


            rolename=$("#rolename").textbox("getValue");
            if(rolename.length ==0 || s.length ==""){
				$.messager.alert("提示","名字没有填写或者菜单没有选择");
				return
			}
			$("#win").mask("数据加载中...");
			axios.post(Network.geturl("/v1/addrole"), {
					name:rolename+"",
					menu_ids:s+""
			}).then(function (response) {
				    $("#win").unmask();
					if(response.data.code ==200){
						$('#win').window('close');
						$("#roleGrid").datagrid("reload");
					}else{
						$.messager.alert("添加失败",response.data.msg);
					}
						
				}).catch(function (error) {
					$("#win").unmask();
					$('#win').window('close');
					$.messager.alert("交易报错",error);
				});

			//end

		},
		updateopen:function(param){
			var mm = Base64.decode(param);
			var yy=JSON.parse(mm);
			console.log(yy);

			$("#tree_menu11").tree({
				url: Network.geturl("/v1/getmenulist?p=")+yy.menu_ids,
				animate:true,
				method:'get',
				animate:true,
				checkbox:true,
				onLoadError:function(err){
                          $.messager.alert('tree','网络故障');
                }
				});
			$('#win').window('open');
			$("#rolename").textbox("setValue",yy.name);
            $("#roleid").attr("data",yy.id+"");
		},
		update:function(){
         //
		 var nodes = $('#tree_menu11').tree('getChecked');
			var s = '';
			for(var i=0; i<nodes.length; i++){
				if(nodes[i].isdir=="1") continue;
				if (s != '') s += ',';
				s += nodes[i].id;
			}
			if (s == ""){
				$.messager.alert("提示","update请选择一个或者多个菜单");
				return;
			}
		   var  roleid=$("#roleid").attr("data");
		   var rolename=$("#rolename").textbox("getValue");

		   if(!roleid|| s.length ==0 || rolename.length ==0){
			   console.log(roleid,s,rolename);
			$.messager.alert("提示","roleid rolename为空");
			   return
		   }
           //begin
		   axios.post(Network.geturl("/v1/editrole"), {
			                            id:roleid+"",
                                        name:rolename+"",
                                        menu_ids:s+""
                               })
			.then(function (response) {
				console.log(response.data);
				if(response.data.code ==200){
					$('#win').window('close');
					$("#roleGrid").datagrid("reload");
				}else{
					$.messager.alert("添加失败",response.data.msg);
				}   
			})
			.catch(function (error) {
				$('#win').window('close');
				$.messager.alert("交易报错",error);
			});

		   //end
		 
		},
		delete:function(param){
			var mm = Base64.decode(param);
			var yy=JSON.parse(mm);

			$.messager.confirm('确认', "确定要删除吗？", function(r){
				if (r==true){
					axios.post(Network.geturl("/v1/deleterole"), {
											ID:yy.id+"",       
								})
					.then(function (response) {
						console.log(response.data);
						if(response.data.code ==200){
							
							$("#roleGrid").datagrid("reload");
						
						}else{
							$.messager.alert("删除失败失败",response.data.msg);
						}	
					})
					.catch(function (error) {
								$('#win').window('close');
								$.messager.alert("交易报错",error);
					});
				
			//end
			}
			

            //
			});
				
	}
}


var dg = $("#roleGrid").datagrid({
	
	    url:Network.geturl("/v1/listroles"),
		dataType:"json",
		pagination: true,
		rownumbers: true,
		pagination:true,  
		fit:true, 
		nowrap: true, 
		striped:true,
		fitColumns:true,
		singleSelect:true,
		striped:true,
		pageSize:10,
		pageList:[10,15,20,25,30,35],
		columns:[[
			{field:'id',title:"id序列",width:'15%',align:'center'},
			{field:'name',title:"角色名称",width:'20%',align:'center'},
			{field:"menu_ids",title:"权限ids",width:'30%',align:'center'},
			{field:'x',title:'操作', width:'35%',
			formatter: function(value,row,index){
			
			var reslut=JSON.stringify(row);
				reslut=Base64.encode(reslut);
				var html = "";
				html += "&nbsp;<i class=\"fa fa-pencil-square-o\" style=\"color:#0767c8;font-size:16px;\" aria-hidden=\"true\" title=\"编辑\"  onclick=\"role.updateopen('"+reslut+"')\"></i>";
				html += "&nbsp;<i class=\"fa fa-trash\" aria-hidden=\"true\" style=\"color:red;font-size:16px;\" title=\"删除\" onclick=\"role.delete('"+reslut+"')\"></i>"; 
		        return html;
				
			}
		}
			]],
			toolbar:"#roletab",
			
			onLoadError:function(msg){

		    }
});
 

	</script>


    
</body>
</html>